const { MilvusClient,DataType } = require('@zilliz/milvus2-sdk-node');
const getTextEmbedding = require('./9.embedding');
//创建客户端
const milvusClient = new MilvusClient({
    address: 'localhost:19530',
    timeout: 100000
})
const db_name = 'blog';
const collection_name = 'user';
const field_name = 'name_vector';
(async function () {
    try {
        await milvusClient.useDatabase({
            db_name
        });
        console.log('已经成功切换到了' + db_name);
        const name1 = '中国';
        const name2 = '伟大';
        const name1_vector = await getTextEmbedding(name1);
        const name2_vector = await getTextEmbedding(name2);
        const users = [
            {
                name:name1,
                name_vector:name1_vector
            },
            {
                name:name2,
                name_vector:name2_vector
            }
        ]
        const insertResult = await milvusClient.insert({
            collection_name,//向哪个集合中插入Entity
            fields_data:users
        })
        console.log(insertResult);

        const searchVector = await getTextEmbedding('雄伟');
        const result = await milvusClient.search({
            collection_name,
            vector:searchVector,
            field_name,
            limit:10,
            output_fields:['id','name']
        })
        console.log(result)
      
    } catch (error) {
        console.log(error)
        await milvusClient.closeConnection();
        process.exit(1)
    }

})()